//
//  XMGTabBar.m
//  百思不得姐
//
//  Created by 杨庆 on 2016/12/30.
//  Copyright © 2016年 杨庆. All rights reserved.
//

#import "XMGTabBar.h"
#import "XMGPublishViewController.h"
@interface XMGTabBar()
@property(nonatomic,strong)UIButton*publishBtn;
@end

@implementation XMGTabBar

-(instancetype)initWithFrame:(CGRect)frame{
	if (self==[super initWithFrame:frame]) {
		
		self.backgroundImage=[UIImage imageNamed:@"tabbar-light"];
		//增加一个发布按钮
		UIButton*publishBtn=[UIButton buttonWithType:UIButtonTypeCustom];
		[publishBtn setBackgroundImage:[UIImage imageNamed:@"tabBar_publish_icon"] forState:UIControlStateNormal];
		[publishBtn setBackgroundImage:[UIImage imageNamed:@"tabBar_publish_click_icon"] forState:UIControlStateSelected];
		// 添加按钮监听
		[publishBtn addTarget:self action:@selector(publishClick) forControlEvents:UIControlEventTouchUpInside];
		
		[self addSubview:publishBtn];
		self.publishBtn = publishBtn;
	}
	return self;
}

-(void)layoutSubviews{
	[super layoutSubviews];
	self.publishBtn.width=self.publishBtn.currentBackgroundImage.size.width;
	self.publishBtn.height=self.publishBtn.currentBackgroundImage.size.height;
	self.publishBtn.frame=CGRectMake(0, 0, self.publishBtn.width, self.publishBtn.height);
	self.publishBtn.center=CGPointMake(self.width*0.5, self.height*0.5);
	
	CGFloat	buttonY =0;
	CGFloat	buttonW = self.width/5;
	CGFloat	buttonH = self.height;
	NSInteger index = 0;
	for (UIView*button in self.subviews) {
		
		if (![button isKindOfClass:[UIControl class]] || button == self.publishBtn) continue;
		
	    CGFloat	buttonX = buttonW * ((index > 1)?(index+1):index);
		button.frame=CGRectMake(buttonX, buttonY, buttonW, buttonH);
	
	    index++;
		}
							 
}


-(void)publishClick{
	XMGPublishViewController*publishVc=[[XMGPublishViewController alloc]init];
	[[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:publishVc animated:NO completion:nil];
}


@end
